﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;

namespace WebApplicationHook
{
    public class PubSubChannelWorkflow
    {
        private static Dictionary<string, ISubscriber> m_SubscriberPool = new Dictionary<string, ISubscriber>();

        private delegate void NotifyDelegate(KeyValuePair<string, ISubscriber> subScriber, int taskType);
        private delegate void SendEmailDelegate(KeyValuePair<string, ISubscriber> subScriber);
        private delegate void TakePhotoDelegate(KeyValuePair<string, ISubscriber> subScriber);

        private NotifyDelegate notifyDelegate;
        private SendEmailDelegate sendEmailDelegate;
        private TakePhotoDelegate takePhotoDelegate;

        public PubSubChannelWorkflow()
        {
            notifyDelegate = this.NotifyHandler;
            sendEmailDelegate = this.SendEmailHandler;
            takePhotoDelegate = this.TakePhotolHandler;
        }

        public bool RegisterSubscriber(string subscriberId, ISubscriber subscriber)
        {
            try
            {
                if (!string.IsNullOrEmpty(subscriberId) && !m_SubscriberPool.ContainsKey(subscriberId))
                {
                    if (subscriber != null)
                        m_SubscriberPool.Add(subscriberId, subscriber);
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool UnRegisterSubscriber(string subscriberId)
        {
            try
            {
                if (!string.IsNullOrEmpty(subscriberId) && m_SubscriberPool.ContainsKey(subscriberId))
                {
                    m_SubscriberPool.Remove(subscriberId);
                }

                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }



        public void NotifySubscribers(int taskType)
        {
            if (m_SubscriberPool != null)
                foreach (var keyValuePair in m_SubscriberPool.Where(x => x.Value != null))
                {
                    //Check in case disconnection
                    if (((ICommunicationObject) keyValuePair.Value).State == CommunicationState.Opened)
                    {
                        notifyDelegate.BeginInvoke(keyValuePair, taskType, null, null);
                    }
                    else
                    {
                        UnRegisterSubscriber(keyValuePair.Key);
                    }

                }
        }

        public void SendEmailSubscribers()
        {
            if (m_SubscriberPool != null)
                foreach (var keyValuePair in m_SubscriberPool.Where(x => x.Value != null))
                {
                    //Check in case disconnection
                    if (((ICommunicationObject)keyValuePair.Value).State == CommunicationState.Opened)
                    {
                        sendEmailDelegate.BeginInvoke(keyValuePair, null, null);
                    }
                    else
                    {
                        UnRegisterSubscriber(keyValuePair.Key);
                    }

                }
        }

        public void TakePhotoSubscribers()
        {
            if (m_SubscriberPool != null)
                foreach (var keyValuePair in m_SubscriberPool.Where(x => x.Value != null))
                {
                    //Check in case disconnection
                    if (((ICommunicationObject)keyValuePair.Value).State == CommunicationState.Opened)
                    {
                        takePhotoDelegate.BeginInvoke(keyValuePair, null, null);
                    }
                    else
                    {
                        UnRegisterSubscriber(keyValuePair.Key);
                    }

                }
        }

        private void NotifyHandler(KeyValuePair<string, ISubscriber> subScriber, int taskType)
        {
            // check in case subScriber is timeout
            try
            {
                subScriber.Value.Notify(taskType);

      
            }
            catch (TimeoutException)
            {
                UnRegisterSubscriber(subScriber.Key);
            }
        }

       

        private void SendEmailHandler(KeyValuePair<string, ISubscriber> subScriber)
        {
            // check in case subScriber is timeout
            try
            {
                subScriber.Value.SendEmail();

            }
            catch (TimeoutException)
            {
                UnRegisterSubscriber(subScriber.Key);
            }
        }

        private void TakePhotolHandler(KeyValuePair<string, ISubscriber> subScriber)
        {
            // check in case subScriber is timeout
            try
            {
                subScriber.Value.TakePhoto();

            }
            catch (TimeoutException)
            {
                UnRegisterSubscriber(subScriber.Key);
            }

        }
    }
}